function varargout = rteeg(varargin)
% RTEEG MATLAB code for rteeg.fig
%      RTEEG, by itself, creates a new RTEEG or raises the existing
%      singleton*.
%
%      H = RTEEG returns the handle to a new RTEEG or the handle to
%      the existing singleton*.
%
%      RTEEG('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in RTEEG.M with the given input arguments.
%
%      RTEEG('Property','Value',...) creates a new RTEEG or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before rteeg_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to rteeg_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help rteeg

% Last Modified by GUIDE v2.5 26-May-2014 15:23:23

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @rteeg_OpeningFcn, ...
                   'gui_OutputFcn',  @rteeg_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before rteeg is made visible.
function rteeg_OpeningFcn(hObject, ~, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to rteeg (see VARARGIN)

clc;
disp('*****************************************************************');
disp('*     Welcome to the Realtime EEG Processing Platform');
disp('*****************************************************************');

% TODO: load the resource files such as the CDATA of some uicontrols from
% the directory '\res'

% TODO: search and load plugins
disp('*     Loading plugins...');

disp('*     Plugins loaded.');
disp('*****************************************************************');

% Display some of the configuration. For detail configuration, please click
% the configuration button
curFolder = fileparts(mfilename('fullpath'));
configFilePath = fullfile(curFolder, 'config\rteegconfig.mat');
handles.configFilePath = configFilePath;
if ~exist(configFilePath, 'file')
    genconfig(configFilePath);
end
load(configFilePath, '-mat');
set(handles.txtFilePath, 'String', Config.Storage.StoragePath);

% Reserve  the handle of the Simulation Timer.
handles.tmrSimulation = [];

% Choose default command line output for rteeg
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes rteeg wait for user response (see UIRESUME)
% uiwait(handles.figMain);

% --- Outputs from this function are returned to the command line.
function varargout = rteeg_OutputFcn(hObject, ~, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Maximize the GUI or move it to the center of the screen
if ispc
    maximize(hObject);
else
    movegui(hObject, 'center');
end

% Get default command line output from handles structure
varargout{1} = handles.output;



% --- Executes on button press in btnStartRecording.
function btnStartRecording_Callback(hObject, eventdata, handles)
% hObject    handle to btnStartRecording (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



function txtFileName_Callback(hObject, eventdata, handles)
% hObject    handle to txtFileName (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtFileName as text
%        str2double(get(hObject,'String')) returns contents of txtFileName as a double


% --- Executes during object creation, after setting all properties.
function txtFileName_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtFileName (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function txtFilePath_Callback(hObject, eventdata, handles)
% hObject    handle to txtFilePath (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtFilePath as text
%        str2double(get(hObject,'String')) returns contents of txtFilePath as a double


% --- Executes during object creation, after setting all properties.
function txtFilePath_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtFilePath (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



% --- Executes on button press in btnAcquisition.
function btnAcquisition_Callback(hObject, eventdata, handles)
% hObject    handle to btnAcquisition (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
disp 'Pressed!'

% --- Executes on button press in btnSimulation.
function btnSimulation_Callback(hObject, ~, handles) %#ok<DEFNU>
% hObject    handle to btnSimulation (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global BVInfo;
global EEGData;
global currentPosition;

% Let the user choose the eeg file for acquisition simulation
[fileName, pathName] = uigetfile({'*.vhdr', 'Brain Vision EEG Header File'}, ...
    'Select Brain Vision Data Exchange Header File');
if fileName == 0
    return;
end

% Change the 'Enable' property of the buttons to avoid unexpected button
% click.
set(hObject, 'Enable', 'off');
set(handles.btnAcquisition, 'Enable', 'on');
set(handles.btnStop, 'Enable', 'on');


% Load the realtime windows length from the configuartion file.
load(handles.configFilePath, '-mat');
windowLengthInSeconds = Config.Amplifier.RealtimeWindowLength;

% Load the detail information of the EEG files. As this is the simulation
% process, some configuarion parameters would be ommited.
[BVInfo, EEGData] = loadbrainvisiondata(fullfile(pathName, fileName));
currentPosition = 1;

nChannels = BVInfo.vhdr.CommonInfos.NumberOfChannels;
samplingRate = BVInfo.vhdr.CommonInfos.SamplingInterval;
nPoints = samplingRate*windowLengthInSeconds;

% Set the trigger interval of the simualtion timer, then start it.
tmrSimulation = timer('Name', 'tmrSimulation', 'ObjectVisibility', 'off', ...
    'ExecutionMode', 'fixedRate', 'BusyMode', 'drop');
handles.tmrSimulation = tmrSimulation ;
guidata(hObject, handles);

set(tmrSimulation, ...
    'Period', windowLengthInSeconds, ...
    'TimerFcn', {@dataAcquisitionSimulation, nChannels, nPoints});
start(tmrSimulation);



% --- Executes on button press in btnStop.
function btnStop_Callback(hObject, ~, handles) %#ok<DEFNU>
% hObject    handle to btnStop (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Change the 'Enable' property of the buttons to avoid unexpected button
% click.
set(handles.btnSimulation, 'Enable', 'on');
set(handles.btnAcquisition, 'Enable', 'on');
set(hObject, 'Enable', 'off');

% Stop the timer and delete the timer objects.
if isvalid(handles.tmrSimulation)
    stop(handles.tmrSimulation);
    delete(handles.tmrSimulation);
end

% Clear the global variables
% TODO: you may want to clear the globals seletively.
clear global;



% --- Executes on button press in btnStopRecording.
function btnStopRecording_Callback(hObject, eventdata, handles)
% hObject    handle to btnStopRecording (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in btnImp.
function btnImp_Callback(hObject, eventdata, handles)
% hObject    handle to btnImp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
uiwait(impedance());



% --- Executes on button press in btnConfig.
function btnConfig_Callback(~, ~, handles) %#ok<DEFNU>
% hObject    handle to btnConfig (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA) 

% open the configuration window and wait for the user 
% complete the configuration.
config;

% after the configuration finished, show part of the configuration in the
% main window.
load(handles.configFilePath, '-mat');
set(handles.txtFilePath, 'String', Config.Storage.StoragePath);



function cboTimeScale_Callback(hObject, eventdata, handles)
% hObject    handle to cboTimeScale (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of cboTimeScale as text
%        str2double(get(hObject,'String')) returns contents of cboTimeScale as a double


% --- Executes during object creation, after setting all properties.
function cboTimeScale_CreateFcn(hObject, eventdata, handles)
% hObject    handle to cboTimeScale (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function cboDisplayScale_Callback(hObject, eventdata, handles)
% hObject    handle to cboDisplayScale (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of cboDisplayScale as text
%        str2double(get(hObject,'String')) returns contents of cboDisplayScale as a double


% --- Executes during object creation, after setting all properties.
function cboDisplayScale_CreateFcn(hObject, eventdata, handles)
% hObject    handle to cboDisplayScale (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function cboChn_Callback(hObject, eventdata, handles)
% hObject    handle to cboChn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of cboChn as text
%        str2double(get(hObject,'String')) returns contents of cboChn as a double


% --- Executes during object creation, after setting all properties.
function cboChn_CreateFcn(hObject, eventdata, handles)
% hObject    handle to cboChn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in chkBaselineCorrection.
function chkBaselineCorrection_Callback(hObject, eventdata, handles)
% hObject    handle to chkBaselineCorrection (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of chkBaselineCorrection


% --------------------------------------------------------------------
function mnuFile_Callback(hObject, eventdata, handles)
% hObject    handle to mnuFile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function itmLoadConfig_Callback(hObject, eventdata, handles)
% hObject    handle to itmLoadConfig (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function itmSaveConfig_Callback(hObject, eventdata, handles)
% hObject    handle to itmSaveConfig (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function itmResetConfig_Callback(hObject, eventdata, handles)
% hObject    handle to itmResetConfig (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function itmShowLog_Callback(hObject, eventdata, handles)
% hObject    handle to itmShowLog (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function itmExit_Callback(hObject, eventdata, handles)
% hObject    handle to itmExit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function mnuEdit_Callback(hObject, eventdata, handles)
% hObject    handle to mnuEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function mnuView_Callback(hObject, eventdata, handles)
% hObject    handle to mnuView (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function mnuHelp_Callback(hObject, eventdata, handles)
% hObject    handle to mnuHelp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function itmOptions_Callback(hObject, eventdata, handles)
% hObject    handle to itmOptions (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes when figMain is resized.
function figMain_ResizeFcn(hObject, eventdata, handles)
% hObject    handle to figMain (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes when user attempts to close figMain.
function figMain_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to figMain (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% Do some cleaning jobs.

% Hint: delete(hObject) closes the figure
delete(hObject);


% --- Executes on selection change in cboChnNum.
function cboChnNum_Callback(hObject, eventdata, handles)
% hObject    handle to cboChnNum (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns cboChnNum contents as cell array
%        contents{get(hObject,'Value')} returns selected item from cboChnNum


% --- Executes during object creation, after setting all properties.
function cboChnNum_CreateFcn(hObject, eventdata, handles)
% hObject    handle to cboChnNum (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --------------------------------------------------------------------
function tlbCheckDevice_ClickedCallback(hObject, eventdata, handles)
% hObject    handle to tlbCheckDevice (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function tlbLoadConfig_ClickedCallback(hObject, eventdata, handles)
% hObject    handle to tlbLoadConfig (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes during object creation, after setting all properties.
function txtBaseStatus_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtBaseStatus (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
if ispc
    set(hObject, 'BackgroundColor', get(0, 'defaultUicontrolBackgroundColor'));
end


% --- Executes during object creation, after setting all properties.
function txtSampleRate_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtSampleRate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
if ispc
    set(hObject, 'BackgroundColor', get(0, 'defaultUicontrolBackgroundColor'));
end


% --- Executes during object creation, after setting all properties.
function txtChnStatus_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtChnStatus (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
if ispc
    set(hObject, 'BackgroundColor', get(0, 'defaultUicontrolBackgroundColor'));
end


% --- Executes during object creation, after setting all properties.
function txtSyncStatus_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtSyncStatus (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
if ispc
    set(hObject, 'BackgroundColor', get(0, 'defaultUicontrolBackgroundColor'));
end
